﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.loading {
    import flash.net.*;
    import flash.utils.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.loading.pipes.*;
    import net.wooga.pets.loading.interfaces.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.loading.loaders.*;

    public class LoaderService extends Actor {

        private var _groupCount:int = 0
        private var _pipeTypes:Object;
        private var _loaderClasses:Object;
        private var _pipes:Dictionary;
        private var _groups:Dictionary;
        private var _loaders:Dictionary;

        public function LoaderService(){
            this._pipeTypes = {};
            this._loaderClasses = {};
            this._pipes = new Dictionary();
            this._groups = new Dictionary();
            this._loaders = new Dictionary();
            super();
            this._loaderClasses[GameConstants.SWF] = SWFLoader;
            this._loaderClasses[GameConstants.JSON] = JSONLoader;
            this._loaderClasses[GameConstants.XML] = XMLLoader;
        }
        public function load(_arg1:Function, _arg2:String, _arg3:Array, _arg4:Function):void{
            var _local6:RequestVO;
            var _local7:IRequestLoader;
            var _local5:ILoaderGroup = this.getGroup(_arg2, _arg1, _arg4);
            for each (_local6 in _arg3) {
                _local7 = this.getLoader(_local6);
                _local5.add(_local7);
                this.startLoader(_local7);
            };
            _local5.setActive();
        }
        private function getPipe(_arg1:String):LoaderPipe{
            this._pipes[_arg1] = ((this._pipes[_arg1]) || (new LoaderPipe()));
            return ((this._pipes[_arg1] as LoaderPipe));
        }
        private function startLoader(_arg1:IRequestLoader):void{
            if (_arg1.isPending){
                if (_arg1.pipeId){
                    this.getPipe(_arg1.pipeId).addLoader(_arg1);
                } else {
                    _arg1.start();
                };
            } else {
                if (_arg1.isFinished){
                    _arg1.loaded.dispatch(_arg1.id, _arg1.result);
                };
            };
        }
        private function getGroup(_arg1:String, _arg2:Function, _arg3:Function):ILoaderGroup{
            _arg1 = ((_arg1) || (("group_" + this._groupCount++)));
            var _local4:ILoaderGroup = (this._groups[_arg1] as ILoaderGroup);
            if (!_local4){
                _local4 = new LoaderGroup(_arg1, _arg2);
                _local4.onGroupError = ((_arg3) || (this.onError));
                _local4.completed.addOnce(this.handleComplete);
                _local4.progressing.add(this.handleGroupProgress);
                this._groups[_arg1] = _local4;
            };
            return (_local4);
        }
        private function getLoader(_arg1:RequestVO):IRequestLoader{
            var _local3:Class;
            var _local4:int;
            var _local2:IRequestLoader = (this._loaders[_arg1.url] as IRequestLoader);
            if (!_local2){
                _local3 = (this._loaderClasses[_arg1.format] as Class);
                _local2 = new (_local3);
                _local2.id = _arg1.id;
                _local2.pipeId = _arg1.pipeId;
                _local2.path = _arg1.url;
                _local2.totalSize = _arg1.fileSize;
                _local2.method = _arg1.method;
                if (_arg1.caching){
                    this._loaders[_arg1.url] = _local2;
                } else {
                    if (_arg1.method == URLRequestMethod.GET){
                        _local4 = Numbers.randomIntBetween(0, int.MAX_VALUE);
                        _arg1.addParam(ProtocolConstants.RANDOM, _local4);
                    };
                };
                _local2.params = _arg1.params;
            };
            return (_local2);
        }
        private function handleGroupProgress(_arg1:String, _arg2:int, _arg3:int):void{
            var _local4:InfoEvent = new InfoEvent(EventConstants.ON_GROUP_PROGRESS, _arg1, {current:_arg2, total:_arg3});
            dispatch(_local4);
        }
        private function handleComplete(_arg1:String):void{
            delete this._groups[_arg1];
        }
        private function onError(_arg1:String, _arg2:ResultVO):void{
            var _local3:InfoEvent = new InfoEvent(EventConstants.ON_LOAD_ERROR, _arg1, _arg2.status);
            dispatch(_local3);
        }

    }
}//package net.wooga.pets.loading 
